// pages/index/productdetail/productdetail.js
let wxparse = require("../../../wxParse/wxParse.js");
//获取应用实例
const app = getApp();

app.func.page({
    data: {
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        authoried: false,
        canshare: false,
        productinfo: '',
        tips:'',
        hidetips:true,
        choose_status:'',
        hiddenbottom:false,
        myinfo:'',
        bnnerlistTop:[],
        current:true
    },

    onLoad: function (options) {
        app.func.onPageLoad(this, options);
        if(options.hiddenbottom){
            this.setData({
                 hiddenbottom:true
            });
        }

        app.func.onPageLoad(this,options);
        if(options.id){
            this.detail(options.id);
        }

        this.tips(options.id);

    },
   


    onSceneInfo: function (data) {
        this.detail(data.id);
        this.tips(data.id);
    },

    tips:function(id){
       app.func.getPromise(`/product/notice/${id}`)
           .then(([code,res])=>{
               if(res.data){
                   this.setData({
                       hidetips:false,
                       tips:res.data
                   });
               }
           })
    },

    detail: function (id) {
        app.func.getPromise('/sku/detail/' + id)
            .then(([code, res]) => {
                console.log(res)
                let bnnerlistTop =[];
                bnnerlistTop = res.data.product_banner
                
                if(bnnerlistTop.length  ==1){
                    this.setData({
                        current :false
                    })
                }
                this.setData({
                    productinfo: res.data,
                    choose_status:res.data.choose_status,
                    key: res.key,
                    bnnerlistTop:res.data.product_banner
                  
                });
                wxparse.wxParse('dkcontent', 'html', res.data.content, this, 5);
            })
    },


    onShow: function () {
        this.myinfos();
        //判断是否授权
        app.func.getuserinfo((userInfo) => {
            this.setData({userinfomation: userInfo, authoried: true});
        }, () => {
            this.setData({authoried: false});
        });
        
    },
    myinfos() {
        app.func.getPromise('/my/info')
            .then(([code, res]) => {
                console.log(res.message)
            this.setData({
                myinfo: res.message
            });
        })
    },

    jumpindex: function () {
        wx.switchTab({
            url: '/pages/index/index'
        })
    },

    addonshopbuy: function () {
        var id = this.data.productinfo.id;
        wx.navigateTo({
			url: '/pages/cartshop/payif/payif?skuid=' + id
	})
    },

    addproduct: function () {
        let id = this.data.productinfo.id;
        app.func.getPromise('/addcart/' + id)
            .then(([code, res]) => {
                if (code == 200) {
                    app.func.toastPromise('加入优选成功','none',1000)
                        .then(()=>{
                        this.collectChange(id);
                    })
                } else if (code == 2001) {
                    app.func.toastPromise('已经加入优选','none',1000)
                } else{
                    app.func.toastPromise(res.message);
                }
            })
    },
    clearChoose:function () {
        let id = this.data.productinfo.id;
        app.func.getPromise('/delcart/'+id)
            .then(([code, res]) => {
                if(code == 200){
                    app.func.toastPromise('移除优选成功','none',1000)
                        .then(()=>{
                            this.collectChange(id);
                        })
                }
            })
    },
    collectChange:function (id) {
        this.detail(id);
    },



    // 加入购物che
    addcart:function(){
        //获取购物车的原数组
        // app.func.getPromise('/cartv2')
        // .then(([code, res]) => {
            // if(code == 200){
            //  let cart = res.data;
            // let index = cart.findIndex(v=>v.id === this.data.productinfo.id);
            // console.log(index)
            // if(index == -1){
            //     //不在购物车里
            //     this.data.productinfo.num =1;
            //     checked:true;
            //     cart.push(this.data.productinfo)
            // }else{
            //     cart[index].num++;
            // }
            // console.log(this.data.productinfo.num)
            app.func.postPromise('/addcartv2',
                    {
                        pro_id:this.data.productinfo.id ,
                        count:1
                    })  .then(([code, res]) => {
                        console.log(res);
                        if(code == 200){
                            wx.showToast({
                              title: '加入成功',
                              icon:'success',
                              mask:true,
                              duration:1000

                            })
                        }else{
                            wx.showToast({
                              title:res.message,
                              icon:'none',
                              mask:true
                            })
                        }
                    })

            // }
        // })


    
    },


    login: function () {
        wx.switchTab({
            url: '/pages/about/about'
        })
    },

    //货源说明
    product_info: function (e) {
        var id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '/pages/index/productdetail/product-description/product-description?id=' + id
        })
    },


    attestation:function(){
        wx.navigateTo({
            url: '/pages/auth/preattestation/preattestation'
        })
    },


    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return {
            title: this.data.productinfo.name,
            imageUrl:this.data.productinfo.pic
        }
    }


});

